﻿using System;
using System.Linq;
using Controllers;
using System.Configuration;

namespace Web
{
    public partial class DocNoEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["DocId"] != null)
                {
                    int id = int.Parse(Request.Params["DocId"]);
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    DocNo model = ctx.DocNos.Single(q => q.Id == id);
                    txtDocName.Text = model.DocName;
                    txtDocNo.Text = model.DocNo1;
                    txtOrder.Text = model.OrderNo.ToString();
                    DocId.Value = id.ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                DocNo model;

                if (string.IsNullOrWhiteSpace(DocId.Value))
                {
                    model = new DocNo();
                    model.DocName = txtDocName.Text.Trim();
                    model.DocNo1 = txtDocNo.Text.Trim();
                    model.OrderNo = int.Parse(txtOrder.Text);
                    model.CreatDate = DateTime.Now;
                    ctx.DocNos.InsertOnSubmit(model);
                }
                else
                {
                    model = ctx.DocNos.Single(q => q.Id == int.Parse(DocId.Value));
                    model.DocName = txtDocName.Text.Trim();
                    model.DocNo1 = txtDocNo.Text.Trim();
                    model.OrderNo = int.Parse(txtOrder.Text);
                }
                ctx.SubmitChanges();
            }
            Response.Write("<script>window.returnValue='1';window.close();</script>");
        }
    }
}